//
//  BeforeView.m
//  DefendHealth
//
//  Created by UNOHACHA on 17/2/22.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "BeforeView.h"

#import "Header.h"


@interface BeforeView ()<UIScrollViewDelegate>




@end


@implementation BeforeView {
    
   UIScrollView *_scrollV;
    
    UIPageControl* _pageCtrl;
    
    UIButton * _selectBut;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = kWhiteColor;
        [self mainViewShared];
        
    }
    
    return self;
}

- (void)mainViewShared {
    
    NSArray * it = @[@"上次训练",@"历史最佳"];
    for (int btn = 0; btn<2; btn++) {
        
      UIButton * seleBut = [UIButton creatBtnWithFrame:CGRectMake(kScreenWidth/2*btn, 0, kScreenWidth/2, 40) btnTitle:it[btn] btnTitleColor:UIColorFromRGB(0x999999) btnTitleFont:0 btnTagPrice:btn+80 btnSetImage:nil btnBackgroundColor:nil isCorner:NO action:^(UIButton *button) {
          
          [self chanBtnSelect:button];
      }];
         seleBut.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];

        [seleBut setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateDisabled];
        
        
        [self addSubview:seleBut];
        if (btn == 0) {
            seleBut.enabled = NO;
            _selectBut = seleBut;
        }
    }
    
   
    _scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kMaxY(_selectBut.frame), kScreenWidth,100)];
    _scrollV.contentSize = CGSizeMake(kScreenWidth*2, 0);
    _scrollV.pagingEnabled = YES;
    _scrollV.bounces = NO;
    _scrollV.delegate = self;
    _scrollV.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scrollV];
    

    _pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.height-20, _scrollV.width, 20)];
    _pageCtrl.numberOfPages = 2;//总的图片页数
    _pageCtrl.currentPage = 0; //当前页
    _pageCtrl.currentPageIndicatorTintColor = UIColorFromRGB(0x969696);
    _pageCtrl.pageIndicatorTintColor = UIColorFromRGB(0xedeceb);
    [self addSubview:_pageCtrl];  //将UIPageControl添加到主界面上。
    
    NSArray * imagA = @[@"trip1",@"big-clock",@"consume1",@"5.20km",@"30分钟",@"250卡路里"];
    for (int B = 0; B<3; B++) {
        UIView * imV = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth/3*B,0, kScreenWidth/3, _scrollV.height)];
        [_scrollV addSubview:imV];
        
        UIImageView * loImg = [ReduceCode creatImageVeiwWithFrame:CGRectMake((imV.height-24)/2,9, 24, 24) imageName:imagA[B] isCorner:NO];
        [imV addSubview:loImg];
        
        UILabel * numL = [ReduceCode creatLabelWithFrame:CGRectMake(0, kMaxY(loImg.frame)+6, imV.width, 14) title:imagA[B+3] titleColor:UIColorFromRGB(0x333333) font:14 textAlignment:NSTextAlignmentCenter];
        numL.centerX = loImg.centerX;
       [imV addSubview:numL];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
     NSInteger OffsetX = scrollView.contentOffset.x/scrollView.width;
//    NSLog(@"!!!!%ld",OffsetX);
    
    [self chanBtnSelect:[self viewWithTag:OffsetX+80]];
}

- (void)chanBtnSelect:(UIButton *)seleBtn {
    
    NSInteger index = seleBtn.tag-80;
    
    [_scrollV setContentOffset:CGPointMake(kScreenWidth*index, 0) animated:YES];
    _pageCtrl.currentPage = index;
    
    _selectBut.enabled = YES;
    seleBtn.enabled = NO;
    _selectBut = seleBtn;
}



@end
